{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "分类标签\n",
       "-1    27415\n",
       " 0      231\n",
       " 1       88\n",
       " 2       37\n",
       " 3      988\n",
       " 4      541\n",
       " 5      245\n",
       " 6      115\n",
       " 7       77\n",
       " 8      233\n",
       " 9       30\n",
       "Name: 分类标签, dtype: int64"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.cluster import DBSCAN\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "\n",
    "data_pretreatment = pd.read_csv(\"../data/order.csv\")\n",
    "data_pretreatment.drop(['customer','order','hour'],axis = 1,inplace=True) \n",
    "\n",
    "# 创建StandardScaler对象并对数据进行标准化\n",
    "scaler = StandardScaler()\n",
    "X_scaled = scaler.fit_transform(data_pretreatment)\n",
    "\n",
    "# 构建DBSCAN聚类模型\n",
    "dbscan = DBSCAN(eps=0.5,min_samples=30) # 最小样本数设为20\n",
    "# 对数据进行聚类分析\n",
    "dbscan.fit(X_scaled)\n",
    "# 提取聚类结果\n",
    "labels = dbscan.labels_\n",
    "# 将标签添加到表格对象中\n",
    "data_pretreatment['分类标签'] = labels\n",
    "# 聚类分析\n",
    "data_pretreatment.groupby(by='分类标签')['分类标签'].count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.13 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "79c2bc36f36ebca92fe5f3c73355a3866f3a34a4fd0b1d43087d5313dd5fd5ff"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
